//Topic管理工具
package com.cuijian.demo.mq;

import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
import org.apache.rocketmq.common.TopicConfig;

public class TopicManager {

    private static final String NAME_SERVER = "localhost:9876";
    private static final String BROKER_ADDR = "localhost:10911"; // 默认Broker地址

    /**
     * 手动创建Topic
     */
    public static boolean createTopic(String topic) {
        DefaultMQAdminExt adminExt = null;
        try {
            adminExt = new DefaultMQAdminExt();
            adminExt.setNamesrvAddr(NAME_SERVER);
            adminExt.start();

            // 创建Topic配置
            TopicConfig topicConfig = new TopicConfig();
            topicConfig.setTopicName(topic);
            topicConfig.setReadQueueNums(4);    // 读队列数
            topicConfig.setWriteQueueNums(4);   // 写队列数
            topicConfig.setPerm(6);             // 权限：6=读写

            // 直接使用默认Broker地址创建Topic
            adminExt.createAndUpdateTopicConfig(BROKER_ADDR, topicConfig);

            System.out.println("Topic创建成功: " + topic);
            return true;

        } catch (Exception e) {
            System.err.println("创建Topic失败: " + topic + ", Error: " + e.getMessage());
            return false;
        } finally {
            if (adminExt != null) {
                adminExt.shutdown();
            }
        }
    }

    /**
     * 检查Topic是否存在（简化版本）
     */
    public static boolean topicExists(String topic) {
        DefaultMQAdminExt adminExt = null;
        try {
            adminExt = new DefaultMQAdminExt();
            adminExt.setNamesrvAddr(NAME_SERVER);
            adminExt.start();

            // 简化的检查方法：尝试创建，如果已存在会抛出异常
            adminExt.examineTopicStats(topic);
            return true;

        } catch (Exception e) {
            // 如果出现异常，说明Topic可能不存在
            return false;
        } finally {
            if (adminExt != null) {
                adminExt.shutdown();
            }
        }
    }

    /**
     * 初始化所有必要的Topic
     */
    public static void initAllTopics() {
        String[] topics = {
                MQConstants.TOPIC_ORDER,
                MQConstants.TOPIC_USER,
                MQConstants.TOPIC_NOTIFICATION
        };

        System.out.println("开始初始化RocketMQ Topic...");

        for (String topic : topics) {
            try {
                if (!topicExists(topic)) {
                    System.out.println("正在创建Topic: " + topic);
                    boolean success = createTopic(topic);
                    if (success) {
                        System.out.println("Topic创建成功: " + topic);
                    } else {
                        System.out.println("Topic创建失败: " + topic);
                    }
                } else {
                    System.out.println("Topic已存在: " + topic);
                }
            } catch (Exception e) {
                System.err.println("处理Topic " + topic + " 时出现异常: " + e.getMessage());
            }
        }

        System.out.println("RocketMQ Topic初始化完成");
    }
}